﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHD.DAL;
using QLHD.BLL;

namespace QLHD
{
    public partial class frmNhapNL_Tho : Form
    {
        string maauto;
        public frmNhapNL_Tho() { }
        public frmNhapNL_Tho(frmMain form)
        {
            InitializeComponent();
            MdiParent = form;
        }

        private void frmNhapNL_Tho_Load(object sender, EventArgs e)
        {
            int index = 0;
            dgvNLTho.DataSource = CTNhapKho_DB.SelectAllNhap_Tho();
            LoadCombobox();
            DisplayData(index);
            ReadOnlyTextbox();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            dtpNgayThang.Text = DateTime.Now.ToString();
            dtpNgayThang.Enabled = false;
            AutoMaNhapNLTho();
            txtMaNhapNL.Text = maauto;
            if (btnThem.Text == "Thêm")
            {
                btnThem.Text = "Lưu";
                NotReadOnlyTextbox();
                txtMaLo.ReadOnly = true;
                txtMaNhom.ReadOnly = true;
                ClearTextbox();

            }
            else
            {
                try
                {
                    InsertNhapNL_Tho();
                    frmNhapNL_Tho_Load(sender, e);    
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtDonGia.Focus();
                }
            }         
        }
        void InsertNhapNL_Tho()
        {
                    CTNhapKho nhaptho = new CTNhapKho();
                    nhaptho.MaNhapNL = txtMaNhapNL.Text;
                    nhaptho.Ngaythang = dtpNgayThang.Text;
                    nhaptho.Manx = txtMaNX.Text;
                    nhaptho.Dongia = Convert.ToDecimal(txtDonGia.Text);
                    nhaptho.Kl_nhap =Convert.ToInt32(txtKhoiLuong.Text);
                    nhaptho.Malo = txtMaLo.Text;
                    nhaptho.Manhom = txtMaNhom.Text;
                    nhaptho.Mancc = cbbNCC.SelectedValue.ToString();
                    nhaptho.Makho = cbbKho.SelectedValue.ToString();
                    nhaptho.Diengiai = txtDienGiai.Text;

                    CTNhapKho_DB.InsertCTNhap_Tho(nhaptho);
                    btnThem.Text = "Thêm";
                    ReadOnlyTextbox();
      
        }
        void ClearTextbox()
        {
            txtDonGia.Clear();
            txtKhoiLuong.Clear();
            txtDienGiai.Clear();
        }
        void LoadCombobox()
        {
            cbbKho.DataSource = Kho_DB.SelectKho();
            cbbKho.DisplayMember = "tenkho";
            cbbKho.ValueMember = "makho";

            cbbNCC.DataSource = NhaCC_DB.selectNCC();
            cbbNCC.DisplayMember = "tenncc";
            cbbNCC.ValueMember = "mancc";
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            try
            {
                CTNhapKho_DB.DeleteCTNhap_Tho(txtMaNhapNL.Text);
                frmNhapNL_Tho_Load(sender, e);
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDonGia.Focus();
            }
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            try
            {
                if (btnSua.Text == "Sửa")
                {
                    btnSua.Text = "Lưu";
                    NotReadOnlyTextbox();
                    txtMaLo.ReadOnly = true;
                    txtMaNhom.ReadOnly = true;
                }
                else
                {
                    CTNhapKho nhaptho = new CTNhapKho();
                    nhaptho.MaNhapNL = txtMaNhapNL.Text;
                    nhaptho.Ngaythang = dtpNgayThang.Text;
                    nhaptho.Manx = txtMaNX.Text;
                    nhaptho.Dongia = Convert.ToDecimal(txtDonGia.Text);
                    nhaptho.Kl_nhap = Convert.ToDouble(txtKhoiLuong.Text);
                    nhaptho.Malo = txtMaLo.Text;
                    nhaptho.Manhom = txtMaNhom.Text;
                    nhaptho.Mancc = cbbNCC.SelectedValue.ToString();
                    nhaptho.Makho = cbbKho.SelectedValue.ToString();
                    nhaptho.Diengiai = txtDienGiai.Text;
                    CTNhapKho_DB.UpdateCTNhap_Tho(txtMaNhapNL.Text, nhaptho);
                    btnSua.Text = "Sửa";
                    frmNhapNL_Tho_Load(sender, e);
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show("Xảy ra lỗi" + "\n" + ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtDonGia.Focus();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            frmNhapNL_Tho_Load(sender, e);            
            btnThem.Text = "Thêm";
            
        }
        void ReadOnlyTextbox()
        {
            txtMaNX.ReadOnly = true;
            txtDonGia.ReadOnly = true;
            txtKhoiLuong.ReadOnly = true;
            txtMaLo.ReadOnly = true;
            txtMaNhom.ReadOnly = true;
            txtDienGiai.ReadOnly = true;
        }
        void NotReadOnlyTextbox()
        {            
            txtDonGia.ReadOnly = false;
            txtKhoiLuong.ReadOnly = false;
            txtMaLo.ReadOnly = false;
            txtMaNhom.ReadOnly = false;
            txtDienGiai.ReadOnly = false;
        }
        void AutoMaNhapNLTho()
        {
            maauto = "";
            SqlConnection con = Connection.GetConnection();
            con.Open();
            string select = "select top 1 MaNhapNL from tblQL_NhapKho_NLTho where NgayThang = '" + dtpNgayThang.Text + "' order by MaNhapNL desc";
            DataTable dt = new DataTable();
            SqlCommand command = new SqlCommand(select, con);
            SqlDataAdapter da = new SqlDataAdapter(command);
            da.Fill(dt);
            
            if (dt.Rows.Count == 0)
            {
                maauto = dtpNgayThang.Text.Replace("/","") + "1";
            }
            else
	        {
                maauto =(Convert.ToInt32(dt.Rows[0][0]) + 1).ToString();
	        }
        }
        void DisplayData(int index)
        {
            txtMaNhapNL.Text = dgvNLTho.Rows[index].Cells[0].Value.ToString();
            dtpNgayThang.Text = dgvNLTho.Rows[index].Cells[1].Value.ToString();
            txtDonGia.Text = dgvNLTho.Rows[index].Cells[6].Value.ToString();
            txtKhoiLuong.Text = dgvNLTho.Rows[index].Cells[7].Value.ToString();
            txtMaLo.Text = dgvNLTho.Rows[index].Cells[3].Value.ToString();
            txtMaNhom.Text = dgvNLTho.Rows[index].Cells[4].Value.ToString();
            cbbNCC.Text = dgvNLTho.Rows[index].Cells[8].Value.ToString();
            txtDienGiai.Text = dgvNLTho.Rows[index].Cells[5].Value.ToString();
            cbbKho.Text = dgvNLTho.Rows[index].Cells[9].Value.ToString();
        }

        private void dgvNLTho_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = e.RowIndex;
                DisplayData(index);
                DateTime dt = Convert.ToDateTime(dgvNLTho.Rows[index].Cells[1].Value);
            }
            catch (Exception)
            {
                return;
            }
            
        }
       
    }
}

